<template>
	<view class="main">


		<view class="hd-box" style="background: url('/static/imgs/bgc-top01.png') no-repeat; background-size: cover;">
			<view class="hd-left">
				<image src="/static/imgs/positioning.png" mode="aspectFill" class="position-icon"></image>
				<view @click="checkAddress()">{{mineAddress}}</view>
				<text class="iconfont" style="color: #fff;">&#xe627;</text>
			</view>
			<view class="hd-right">
				<view class="r-item" @click="navTo('/pages/public/news')">
					<image src="/static/imgs/message.png" mode="aspectFill" class="message"></image>
					<view class="point">
						{{ label.messageNums | handleLabel}}
					</view>
				</view>
				<view class="r-item" @click="navTo('/pages/public/collect')">
					<image src="/static/imgs/collection.png" mode="aspectFill" class="collection"></image>
					<view class="point">
						{{ label.collectCountNums | handleLabel}}
					</view>
				</view>
				<view class="r-item" @click="navTo('/pages/discountMall/shopCart')">
					<image src="/static/imgs/home-shoppingcart.png" mode="aspectFill" class="shoppingcart"></image>
					<view class="point">
						{{ label.cartCountNums | handleLabel}}
					</view>
				</view>
			</view>
		</view>

		<!-- 搜索部分 -->
		<view class="in-header" style="background: url('/static/imgs/bgc-top02.png') no-repeat; background-size: cover;">
			<!-- search 框 -->
			<view class="search-box" @click="getBanner">
				<view class="sb-left" @click="afterFlag = !afterFlag">
					<text class="iconfont">&#xe6b2;</text>
					<view class="left-footer">
						核销
					</view>
				</view>
				<text class="iconfont search">&#xe62e;</text>
				<input type="text" disabled="true" @click="navTo('/pages/index/search')" placeholder="护肤品" placeholder-class="search-input" />

				<!-- 核销弹窗部分 112 160-->
				<view class="popup-after" v-show="afterFlag" style="background: url('/static/imgs/index/background.png') no-repeat; background-size: contain;">
					<view class="line mt" @click="onScan">
						扫一扫
					</view>
					<view class="b"></view>
					<view class="line" @click="navTo('/pages/index/cancelAfter')">
						请输入
					</view>
				</view>

			</view>

		</view>

		<!-- banner 轮播 -->
		<view class="banner">

			<uni-swiper-dot :info="bannerList" :current="current" mode="round" :dots-styles="dotsStyles" field="content">
				<swiper class="swiper-box" @change="change" autoplay="false" interval="50000" circular="true">

					<swiper-item v-for="(item,index) in bannerList" :key="index">
						<view class="uni-bg-blue swiper-item">
							<image :src="item.banimg" mode="aspectFill" />
						</view>
					</swiper-item>

				</swiper>
			</uni-swiper-dot>
		</view>

		<!-- 品牌描述 -->
		<view class="brand">
			<view class="brand-box">
				<view class="box-item">
					<image src="/static/imgs/brand1.png" mode=""></image>
					<text>邻近特价</text>

				</view>
				<view class="box-item">
					<image src="/static/imgs/brand2.png" mode=""></image>
					<text>货真价实</text>

				</view>
				<view class="box-item">
					<image src="/static/imgs/brand3.png" mode=""></image>
					<text>快速到手</text>

				</view>
				<view class="box-item">
					<image src="/static/imgs/brand4.png" mode=""></image>
					<text>无忧无虑</text>
				</view>

			</view>
		</view>


		<!-- 分类栏 -->
		<view class="sort">
			<!-- :scroll-into-view="tabIndex" -->
			<scroll-view scroll-x="true" class="scroll">
				<view class="scroll2">
					<view @click="ontabtap($event,item.cateid)" class="sort-item" v-for="(item,index) in sortList" :key="index" :id="item.cateid"
					 :data-current="item.cateid">
						<text :class="tabIndex==item.cateid ? 'sort-active' : ''">{{item.catename}}</text>
					</view>
				</view>
			</scroll-view>
		</view>

		<swiper class="swiper-box1" :current="scrollInto" @change="ontabChange" :indicator-dots="false" :autoplay="false"
		 duration="300">
			<!-- 9.9专区 -->
			<swiper-item v-for="(a,subs) in sortList" :key="subs">

				<scroll-view class="scroll-view scroll-v" scroll-y="true">
					<!-- <scroll-view scroll-y="true" >
						<view class="a"></view>
					</scroll-view> -->
					<view class="goods-box">
						<view class="goods-item" v-for="(val,index) in allData" :key="index">
							<view class="sort1" v-if="val.product_cateid == 5">
								<image class="miaosha" src="/static/imgs/index/miaosha.png" mode="aspectFill"></image>
							</view>

							<view class="sort1" v-if="val.product_cateid == 8">
								<image class="pingtuan" src="/static/imgs/index/pingtuan.png" mode="aspectFill"></image>
							</view>

							<view class="sort1" v-if="val.product_cateid == 9">
								<image class="ttc" src="/static/imgs/index/ttc.png" mode="aspectFill"></image>
							</view>

							<view class="sort1" v-if="val.product_cateid == 10">
								<image class="ershou" src="/static/imgs/index/ershou.png" mode="aspectFill"></image>
								<view class="num">
									3888.1
								</view>
							</view>

							<image :src="val.product_proimgs" @click="navTo(`/pages/detail/detail?proid=${val.product_id}`)" mode="aspectFill"
							 class="item-img"></image>
							<view class="item-title">
								{{val.product_name}}
							</view>
							<view class="item-msg">
								<text class="price-1">￥<text class="price-1-1">{{val.product_nowprice * 1}}</text></text>
								<text class="price-2">￥{{val.product_preprice * 1}}</text>
								<text class="msg-3">仅剩{{val.product_pronums}}</text>
								<text class="msg-4" @click="navToOrder(val.shop_id)">抢购</text>
							</view>

							<view class="item-position">
								<view class="left">
									<image :src="val.shop_shavatar" mode="aspectFill" class="user-icon"></image>
									<text class="position-1">{{val.shop_name}}</text>
								</view>
								<view class="right">
									<image src="/static/imgs/people.png" mode="aspectFill" class="person-icon"></image>
									<text class="position-3">{{val.distance}}米</text>
								</view>
							</view>

							<view class="item-footer">
								<view class="footer-l">
									<block v-for="(img,sub) in val.order_user_shavatar" :key="sub">
										<image :src="img.shavatar" mode="aspectFill" class="f-image-1"></image>
									</block>
								</view>
								<view class="footer-r">
									等邻居都在抢
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</swiper-item>

		</swiper>
		<!-- <view class="box-demo"></view> -->
		<view class="mask" v-show="afterFlag" @click="onMask" @touchmove="onMask"></view>

		<!-- 弹窗1 -->
		<QSPopup ref="QSPopup1" type="fadeInMiddle">
			<view class="check failed">
				<view class="line1">
					提货码核对失败！
				</view>
				<view class="line2">
					{{errMsg}}
				</view>
				<button type="primary" @click="onConfirm">确认</button>
			</view>
		</QSPopup>
		<!-- 弹窗2 -->
		<QSPopup ref="QSPopup2" type="fadeInMiddle">
			<view class="check failed">
				<view class="line3">
					核销成功！
				</view>

				<button type="primary" @click="onConfirm" class="confirmButtom">确认</button>
			</view>
		</QSPopup>


	</view>

</template>

<script>
	var bitmap = null;
	import uniSwiperDot from '@/components/uni-swiper-dot/uni-swiper-dot.vue';
	import QSPopup from '@/components/QS-popup/QS-popup.vue';
	import {
		mapState,
		mapMutations
	} from 'vuex';

	export default {
		components: {
			uniSwiperDot,
			QSPopup
		},
		data() {
			return {
				afterFlag: false, //核销
				hxValue: '', // 核销码
				errMsg: '请重新输入提货码！', // 核销失败提示
				mineAddress: '请选择位置', // 位置获取
				LL: {
					long: '114.121224',
					lat: '22.627038'
				}, // 经纬度
				// banner
				bannerList: [], // 轮播数据
				sortList: [], // 分类数据
				sortList1: [{
						id: 'one',
						name: '9.9专区'
					},
					{
						id: 'two',
						name: '限时秒杀'
					},
					{
						id: 'three',
						name: '拼团'
					},
					{
						id: 'fore',
						name: '土特产'
					},
					{
						id: 'five',
						name: '二手'
					},
					{
						id: 'six',
						name: '更多···'
					}
				],

				allData: [], // 所有数据

				zq9: [], // 9.9专区
				seckill: [], // 秒杀
				fight: [], // 拼团
				local: [], // 土特产
				usedGood: [], // 二手
				more: [], //更多
				label: {}, // 点标记

				tabIndex: 1, // 分类ID
				scrollInto: 0, // 轮播的位置
				current: 0,
				mode: 'nav',
				dotsStyles: {
					backgroundColor: 'rgba(255, 255, 255, .4)',
					border: '0px rgba(255, 255, 255, .4) solid',
					color: '#fff',
					selectedBackgroundColor: 'rgba(255, 255, 255, 1)',
					selectedBorder: '0px rgba(255, 255, 255, 1) solid'
				}
			};
		},


		onLoad() {
			this.getBanner();
			this.getSort();
			this.getData();
			this.getLL();
			this.handleAddress();
		},
		filters: {
			handleLabel: (val) => {
				let a = Number(val)
				if (a < 100) {
					a = Number(val);
				} else {
					a = '···'
				}
				return a;
			}
		},
		onPullDownRefresh() {
			setTimeout(() => {
				uni.stopPullDownRefresh();
				uni.showToast({
					title: '刷新成功',
					icon: "none"
				});
			}, 1000)
		},

		onShow() {
			let token = uni.getStorageSync('lph-token');
			if (token) {
				this.getCartAndCollectNums();
			}

		},

		methods: {
			...mapMutations(['changeLogin', 'changeUserInfo']),
			async getData() { // 获取数据
				console.log(this.tabIndex);
				let a = await this.$http.getProductListView({
					lng: this.LL.long,
					lat: this.LL.lat,
					cate_id: this.tabIndex
				});
				this.allData = a.data;
			},
			async getSort() { // 获取分类
				let sort = await this.$http.getIndexSort();
				this.sortList = sort.data;
			},
			async getCartAndCollectNums() { // 点标记数据
				let a = await this.$http.getCartAndCollectNums();
				this.label = a.data;
			},
			async saveDefaultAddress(result) { // 设置地址

				let a = await this.$http.saveDefaultAddress(result);

			},


			onScan() { // 点击扫一扫
				this.afterFlag = false;
				uni.scanCode({
					success: (res) => {
						this.$http.scanCode({
							pullword: res.result
						}).then((a) => {
							if (a.code == 200) {
								this.$refs['QSPopup2'].show();
							} else {
								this.errMsg = a.message;
								this.$refs['QSPopup1'].show();
							}
						})
					}
				})
			},
			onConfirm() { // 核销弹窗内的确定
				this.$refs['QSPopup1'].hide();
				this.$refs['QSPopup2'].hide();
			},
			getLL() { // 获取经纬度，并保存到缓存中
				// #ifdef APP-PLUS
				uni.getLocation({
					type: 'wgs84',
					success: function(res) {
						uni.setStorageSync("lph-ll", {
							long: res.longitude,
							lat: res.latitude
						});
						this.LL = {
							long: res.longitude,
							lat: res.latitude
						}
					}
				});
				// #endif

				// #ifndef APP-PLUS
				uni.setStorageSync("lph-ll", {
					long: '114.121224',
					lat: '22.627038'
				});
				this.LL = {
					long: '114.121224',
					lat: '22.627038'
				};
				// #endif

			},
			checkAddress() { // 选择位置
				uni.chooseLocation({
					success: (res) => {
						this.mineAddress = res.name;
						uni.setStorageSync('lph-address', {
							name: res.name,
							long: res.longitude,
							lat: res.latitude
						});
						// let result = {
						// 	currentAdresss: '',
						// 	lng: '',
						// 	lat: ''
						// }
						// this.
					}
				})
			},
			// 轮播数据
			async getBanner() { // 获取轮播数据
				let a = await this.$http.getBannerList();
				if (a.code == 200) {
					this.bannerList = a.data;
				}
			},

			navTo(url) {
				this.afterFlag = false;
				uni.navigateTo({
					url: url
				})
			},
			async navToOrder(val) { // 加入购物车
				let data = {
					or_proid: val.proid,
					or_shopid: val.shopid,
					or_pronum: 1,
				};
				let a = await this.$http.addOrder(data);
				uni.navigateTo({
					url: `/pages/discountMall/finishOrder?orderId=${val.proid}`
				})
			},
			ontabChange(e) { // banner 切换的
				let index = e.target.current || e.detail.current;
				this.tabIndex = [1, 5, 8, 9, 10][index];
				this.getData();
			},
			ontabtap(e, cateid) { // nav 切换的
				this.tabIndex = cateid;
				this.scrollInto = [1, 5, 8, 9, 10].indexOf(cateid);
			},

			// 改变轮播点标记的
			change(e) {
				this.current = e.detail.current
			},

			onMask() { //  蒙版的处理
				this.afterFlag = false
			},
			handleAddress() { // 处理地址
				let a = uni.getStorageSync("lph-address");
				if ('name' in a) {
					this.mineAddress = a.name;
					return
				}
				this.mineAddress = '请选择地址'
			}

		},




	}
</script>

<style lang="scss" scoped>
	@import 'static/css/index.scss';
	@import 'static/css/doenCommon.scss';
</style>
